package com.yakami.ocean.view.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.annimon.stream.Stream;
import com.jakewharton.rxbinding.view.RxView;
import com.yakami.ocean.R;
import com.yakami.ocean.adapter.base.BaseListAdapter;
import com.yakami.ocean.bean.base.Entity;
import com.yakami.ocean.view.fragment.base.BaseDialogFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author Yakami, Created on 2016/4/18
 */
public class TextListDialogFragment extends BaseDialogFragment {

    @Bind(R.id.recyclerview) RecyclerView mRecyclerView;
    @Bind(R.id.loading) RelativeLayout mLoadingLayout;
    @Bind(R.id.dialog_title) TextView mTitle;
    @Bind(R.id.error_layout) RelativeLayout mErrorLayout;
    @Bind(R.id.text_error) TextView mErrorText;

    private BaseListAdapter mAdapter;
    protected OnRefreshListener mRefreshListener;
    protected BaseListAdapter.OnItemClickListener mClickListener;
    protected String mTitleStr;

    public static final int LOADING = 1;
    public static final int ERROR = 2;
    public static final int SUCCESS = 3;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_list, container);
        ButterKnife.bind(this, view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mAppContext);
        linearLayoutManager.setAutoMeasureEnabled(true);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        if (mClickListener != null)
            mAdapter.setOnItemClickListener(mClickListener);
        mTitle.setText(mTitleStr);
        mRecyclerView.setAdapter(mAdapter);
        mRefreshListener.requestData();
        return view;
    }

    public void setAdapter(BaseListAdapter adapter) {
        mAdapter = adapter;
    }

    public void setTitle(String title) {
        mTitleStr = title;
    }

    public <T extends Entity> void setData(List<T> list) {
        List<Entity> tmp = new ArrayList<>();
        Stream.of(list)
                .forEach(tmp::add);
        mAdapter.addItem(tmp);
    }

    public void onSuccess() {
        switchLoadState(SUCCESS);
    }

    private void switchLoadState(int status) {
        mLoadingLayout.setVisibility(status == LOADING ? View.VISIBLE : View.GONE);
        mErrorLayout.setVisibility(status == ERROR ? View.VISIBLE : View.GONE);
        mRecyclerView.setVisibility(status == SUCCESS ? View.VISIBLE : View.GONE);
    }

    public void onError() {
        switchLoadState(ERROR);
        RxView.clicks(mErrorText)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> {
                    mRefreshListener.requestData();
                    switchLoadState(LOADING);
                });
    }

    public void setOnItemClickListener(BaseListAdapter.OnItemClickListener listener) {
        mClickListener = listener;
    }

    public void setOnRefreshListener(OnRefreshListener listener) {
        mRefreshListener = listener;
    }

    public interface OnRefreshListener {
        void requestData();
    }
}
